package com.huawei.esdk.uc.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.interceptor.LoggingMessage;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

import com.huawei.esdk.platform.common.MessageContext;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.bean.log.LogBean;
import com.huawei.esdk.platform.common.constants.ESDKConstant;
import com.huawei.esdk.platform.common.constants.ESDKErrorCodeConstant;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.flowcontrol.itf.IMonitor;
import com.huawei.esdk.platform.flowcontrol.itf.IPerformer;
import com.huawei.esdk.platform.log.itf.LogInterface;

public class UCInterceptor extends AbstractPhaseInterceptor<Message>
{
    private static org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
    .getLogger(UCInterceptor.class);
    
    public UCInterceptor()
    {
        super(Phase.RECEIVE);
    }
    
    @Override
    public void handleMessage(Message message)
        throws Fault
    {
        // 通知流量监控模块
        IMonitor monitor = ApplicationContextUtil.getBean("northFCMonitor");
        if (monitor != null)
        {
            monitor.reportStatus(1); // 增加一次SOAP调用
            IPerformer performer = ApplicationContextUtil.getBean("northFCPerformer");
            if (performer != null)
            {
                if (performer.doFilter(message))
                {
                    monitor.reportStatus(-1); // 被流控消息不计入监控数据
                    LOGGER.info("*****北向消息，线程id=*****" + Thread.currentThread().getId() + "被流控");
                    SOAPException soapExc = new SOAPException("");
                    Fault fault = new Fault(soapExc);
                    fault.setFaultCode(new QName(Integer.toString(ESDKErrorCodeConstant.ERROR_CODE_SDK_SYSBUSY)));
                    throw fault;
                }
            }
        }
        
        HttpServletRequest req = (HttpServletRequest)message.get("HTTP.REQUEST");
        String ip = req.getRemoteAddr();
        MessageContext mc = ThreadLocalHolder.get();
        if (null == mc)
        {
            mc = new MessageContext();
            ThreadLocalHolder.set(mc);
        }
        mc.getEntities().put(ESDKConstant.ESDK_CLIENT_IP, ip);
        
        String soapAction = req.getHeader("SOAPAction");
        if (null != soapAction)
        {
            String interfaceName = soapAction.substring(1, soapAction.length() - 1);
            // 记录调用日志
            String messageId = (String)message.getExchange().get(LoggingMessage.ID_KEY);
            if (messageId == null)
            {
                messageId = LoggingMessage.nextId();
                message.getExchange().put(LoggingMessage.ID_KEY, messageId);
            }
            
            LogBean bean = new LogBean();
            bean.setActionName(interfaceName);
            bean.setRequestTime(new Date());
            bean.setIp(req.getRemoteHost());
            bean.setPort(req.getRemotePort() + "");
            LogInterface log = ApplicationContextUtil.getBean("logManager");
            log.saveRequestLog(messageId, bean);
        }
        
    }
}
